package cn.gwm.flink.streaming.client;

import cn.gwm.utils.ConfigLoader;
import cn.hutool.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.util.*;

/**
 * @author Liu.Nanfang
 * @date 2022/10/17 16:22
 * @description redis 客户端
 */
public class RedisClient {
    public static JedisCluster jedisCluster;
    private static Integer longTime = 3;

    private static Set<HostAndPort> nodes = new HashSet<>();

    static {
        try {
            String redisCluster = ConfigLoader.get("redis.cluster");
            if (StringUtils.isNotBlank(redisCluster)) {
                String[] split = redisCluster.split(",");
                Arrays.stream(split).forEach(s -> {
                    String[] split1 = s.split(":");
                    nodes.add(new HostAndPort(split1[0], Integer.valueOf(split1[1])));
                });
            }
            String password = ConfigLoader.get("redis.cluster.password");
            // 内网无
            if (StringUtils.isBlank(password)) {
                jedisCluster = new JedisCluster(nodes);
            } else {
                JedisPoolConfig config = new JedisPoolConfig();
                config.setMaxTotal(500);
                config.setMinIdle(2);
                config.setMaxIdle(500);
                config.setMaxWaitMillis(10000);
                config.setTestOnBorrow(true);
                config.setTestOnReturn(true);
                jedisCluster = new JedisCluster(nodes, 10000, 10000, 100, ConfigLoader.get("redis.cluster.password"), config);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 获取数据
     *
     * @param key
     * @return
     */
    public static String getData(String key) {
        return jedisCluster.get(key);
    }

    /**
     * 追加数据
     *
     * @param key
     * @param value
     * @return
     */
    public static boolean putData(String key, String value) {
        return jedisCluster.append(key, value) != null;
    }

    /**
     * 将数据插入队列尾部
     *
     * @param key
     * @param value
     * @return
     */
    public static boolean rpush(String key, List<String> value) {
        for (int i = 0; i < value.size(); i++) {
            jedisCluster.rpush(key, value.get(i));
        }
        return true;
    }

    /**
     * 从队列获取所有数据
     *
     * @param key
     * @return
     */
    public static List<String> lrange(String key) {
        return jedisCluster.lrange(key, 0, -1);
    }

    /**
     * 删除key
     *
     * @param vin
     */
    public static void delKey(String vin) {
        jedisCluster.del(vin);
    }

    /**
     * 根据key 添加 value 并设置过期时间
     *
     * @param key
     * @param val
     * @param time
     */
    public static void setData(String key, String val, long time) {
        // NX 当key 不存在的时候 set，XX 当key存在的时候 set  EX  秒  PX 毫秒
        String nxxx = "NX";
        if (jedisCluster.exists(key)) {
            nxxx = "XX";
        }
        jedisCluster.set(key, val, nxxx, "EX", time);
    }

    public static void hset(String key, String hashKey, String value, int seconds) {
        jedisCluster.hset(key, hashKey, value);
        jedisCluster.expire(key, seconds);
    }

    public static Map<String, String> hgetAll(String key){
        return jedisCluster.hgetAll(key);
    }

    public static void hdel(String key, String hashKey){
        jedisCluster.hdel(key,hashKey);
    }


}